package com.test.spring.jdbc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2018/9/28 16:00
 * <p>
 * 不推荐使用JdbcTemplate，而推荐直接使用JdbcTemplate作为Dao类的成员变量
 */
@Repository
public class PersonDao2 extends JdbcDaoSupport {

    @Autowired
    public void setDataSource2(DataSource source) {
        setDataSource(source);
    }

    public Person get(Integer id) {
        String sql = "SELECT id, name, age FROM persons WHERE id=?";
        RowMapper<Person> rowMapper = new BeanPropertyRowMapper<>(Person.class);
        return getJdbcTemplate().queryForObject(sql, rowMapper, id);
    }
}
